package cn.breeze.pet.controller;

import cn.breeze.basic.dto.HandlerMsgDto;
import cn.breeze.basic.utils.LoginContext;
import cn.breeze.org.domain.Employee;
import cn.breeze.pet.service.ISearchMasterMsgService;
import cn.breeze.pet.domain.SearchMasterMsg;
import cn.breeze.pet.query.SearchMasterMsgQuery;
import cn.breeze.basic.utils.AjaxResult;
import cn.breeze.basic.utils.PageList;
import cn.breeze.user.domain.Logininfo;
import cn.breeze.user.domain.User;
import cn.breeze.user.service.IUserService;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import cn.breeze.basic.exception.BusinessException;
import cn.breeze.basic.annotation.PreAuthorize;

import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private  ISearchMasterMsgService searchMasterMsgService;
    @Autowired
    private IUserService userService;


    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "保存searchMasterMsg", sn = "searchMasterMsg:save")
    public AjaxResult addOrUpdate(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.update(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "删除searchMasterMsg", sn = "searchMasterMsg:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.remove(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试1");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "获取searchMasterMsg", sn = "searchMasterMsg:get")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "获取所有searchMasterMsg", sn = "searchMasterMsg:loadAll")
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @PreAuthorize(name = "列表searchMasterMsg", sn = "searchMasterMsg:list")
    public PageList<SearchMasterMsg> queryData(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.queryData(query);
    }

    /**
    * @Title: patchRemove
    * @Description: 批量删除
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/18 16:57
    * @Parameters: [ids]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PatchMapping
    @PreAuthorize(name = "批量删除searchMasterMsg", sn = "searchMasterMsg:patchDelete")
    public AjaxResult patchRemove(@RequestBody Long[] ids){
        try {
            searchMasterMsgService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试试！");
        }
    }

    /**
    * @Title: publish
    * @Description: 宠物寻主
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/20 11:10
    * @Parameters: [msg, request]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/publish")
    @PreAuthorize(name = "宠物寻主", sn = "searchMasterMsg:publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg msg, HttpServletRequest request)
    {
        try {
            /*UserInfo userInfo = LoginContext.getLoginUser(request);
            Logininfo logininfo = userInfo.getLogininfo();
            User user = userService.loadByLogininfoId(logininfo.getId());*/
            User user = LoginContext.getUser(request);
            msg.setUserId(user.getId());
            searchMasterMsgService.publish(msg);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    @PostMapping("/user")
//    @PreAuthorize(name = "用户自己发布的寻主消息", sn = "searchMasterMsg:user")
    public PageList<SearchMasterMsg> user(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        User user = LoginContext.getUser(request);
        query.setUserId(user.getId());
        return searchMasterMsgService.queryData(query);
    }

    @PostMapping("/toAudit")
//    @PreAuthorize(name = "寻主消息待审核", sn = "searchMasterMsg:toAudit")
    public PageList<SearchMasterMsg> toAudit(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(0);
        return searchMasterMsgService.queryData(query);
    }

    @PostMapping("/toHandle")
//    @PreAuthorize(name = "寻主消息待处理", sn = "searchMasterMsg:toHandle")
    public PageList<SearchMasterMsg> toHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        Employee employee = LoginContext.getEmployee(request);
        query.setShopId(employee.getShopId());
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    @PostMapping("/pool")
    /*@PreAuthorize(name = "寻主池", sn = "searchMasterMsg:pool")*/
    public PageList<SearchMasterMsg> pool(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(1);
        return searchMasterMsgService.queryData(query);
    }

    @PostMapping("/finish")
    /*@PreAuthorize(name = "寻主消息已完成", sn = "searchMasterMsg:finish")*/
    public PageList<SearchMasterMsg> finish(@RequestBody SearchMasterMsgQuery query)
    {
        query.setState(2);
        return searchMasterMsgService.queryData(query);
    }

    /**
    * @Title: accept
    * @Description: 店铺员工接单
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/20 14:50
    * @Parameters: [handlerMsgDto]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/accept")
    /*@PreAuthorize(name = "宠物寻主", sn = "searchMasterMsg:publish")*/
    public AjaxResult accept(@RequestBody HandlerMsgDto handlerMsgDto)
    {
        try {
            searchMasterMsgService.accept(handlerMsgDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

    /**
    * @Title: reject
    * @Description: 拒绝
    * @Author: Mr.D
    * @Version: 1.0
    * @Date:  2022/7/20 16:34
    * @Parameters: [id]
    * @Return cn.breeze.basic.utils.AjaxResult
    */
    @PostMapping("/reject/{id}")
    /*@PreAuthorize(name = "拒绝消息", sn = "searchMasterMsg:reject")*/
    public AjaxResult reject(@PathVariable("id")Long id)
    {
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试");
        }
    }

}
